﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tool.Log;

namespace zdmacs
{
    public class FileUtils
    {

        public static List<string> ReadFileAsList(string fullUpFilePath)
        {
            try
            {
                StreamReader objReader = new StreamReader(fullUpFilePath);
                string sLine = "";
                List<string> LineList = new List<string>();
                while (sLine != null)
                {
                    sLine = objReader.ReadLine();
                    if (sLine != null && !sLine.Equals(""))
                    {
                        //if (!LineList.Contains(sLine))
                        {
                            LineList.Add(sLine);
                        }
                    }
                }
                objReader.Close();
                return LineList;
            }
            catch(Exception ex)
            {
                return null;
            }
        }

        public static List<string> ReadFileAllAsList(string fullUpFilePath)
        {
            try
            {
                StreamReader objReader = new StreamReader(fullUpFilePath);
                string sLine = "";
                List<string> LineList = new List<string>();
                while (sLine != null)
                {
                    sLine = objReader.ReadLine();

                    LineList.Add(sLine);

                }
                objReader.Close();
                return LineList;
            }
            catch
            {
                return null;
            }
        }


        public static string GetCrc(string code)
        {
            try
            {
                int index = 0;
                int count = 0;
                while (code.Length > index)
                {
                    string hex = code.Substring(index, 1);
                    int value = Convert.ToInt32(hex);
                    count = count + value * 3;
                    index = index + 1;
                }
                string hCount = count.ToString();
                return hCount.Substring(hCount.Length - 1, 1);
            }
            catch (Exception ex) {
                return "-1";
            }
            //return hCount.Substring(0, 1);
        }


        public static string WriteTxtToFile(string cid, string lot, string content, string txtFilePath)
        {
            if (!System.IO.Directory.Exists(txtFilePath))
            {
                System.IO.Directory.CreateDirectory(txtFilePath);//不存在就创建目录
            }
            if (!System.IO.Directory.Exists(txtFilePath + "/" + cid))
            {
                System.IO.Directory.CreateDirectory(txtFilePath + "/" + cid);//不存在就创建目录
            }
            try
            {

                FileStream fs = new FileStream(txtFilePath + "/" + cid + "/" + lot + ".txt", FileMode.Create);
                //获得字节数组  
                byte[] data = System.Text.Encoding.UTF8.GetBytes(content);
                //开始写入  
                fs.Write(data, 0, data.Length);
                //清空缓冲区、关闭流  
                fs.Flush();
                fs.Close();
                LogHelper.WriteLog("写入文件：" + txtFilePath + "/" + cid + "/" + lot + ".txt");
                //Form1.writelog("写入文件：" + txtFilePath + "/" + cid + "/" + lot + ".txt");

                return txtFilePath + "/" + cid + "/" + lot + ".txt";
            }
            catch(Exception ex) {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return null;
            }


        }

        public static long gettime()
        {
            DateTime timeStamp = new DateTime(1970, 1, 1);  //得到1970年的时间戳  
            long a = (DateTime.UtcNow.Ticks - timeStamp.Ticks) / 10000000;  //注意这里有时区问题，用now就要减掉8个小时  
            return a;
        }

        public static string[] getRandomNum(int num)
        {
            string[] arrNum = new string[num];
            for (int i = 0; i <= num - 1; i++)
            {
                /**
                long lTick = DateTime.Now.Ticks;
                System.Text.StringBuilder newRandom = new System.Text.StringBuilder(8);
                for (int j = 0; j < 8; j++)
                {
                    Random rd = new Random((int)lTick * j + i);
                    //newRandom.Append(constant[rd.Next(9)]);
                    newRandom.Append(rd.Next(9));
                    lTick += (new Random((int)lTick).Next(978));
                }
                arrNum[i] = newRandom.ToString();
                **/
                arrNum[i] = RandCode(8);
            }
            return arrNum;
        }
        public static string RandCode(int N)
        {
            char[] arrChar = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
            StringBuilder num = new StringBuilder();
            Random rnd = new Random(Guid.NewGuid().GetHashCode());
            for (int i = 0; i < N; i++)
            {
                num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
            }
            return num.ToString();
        }

        public static string AsciiRandCode(int N)
        {
              char[] arrChar = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C','D', 'E', 'F', 'G', 'H', 'J', 'K' , 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y'};
            StringBuilder num = new StringBuilder();
            Random rnd = new Random(Guid.NewGuid().GetHashCode());
            for (int i = 0; i < N; i++)
            {
                num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
            }
            return num.ToString();
        }

        public static char[] constant =   
      {   
        '0','1','2','3','4','5','6','7','8','9'
      };
        public static string getNum(int Length)
        {
            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(8);
            for (int i = 0; i < Length; i++)
            {
                long lTick = DateTime.Now.Ticks;
                Random rd = new Random((int)lTick * i);
                newRandom.Append(constant[rd.Next(9)]);
            }
            return newRandom.ToString();
        }

        public static int[] getRandomNum2(int num)
        {
            int minValue = 10000000;
            int maxValue = 99999999;
            Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
            int[] arrNum = new int[num];
            int tmp = 0;
            for (int i = 0; i <= num - 1; i++)
            {
                tmp = ra.Next(minValue, maxValue); //随机取数
                arrNum[i] = getNum2(arrNum, tmp, minValue, maxValue, ra); //取出值赋到数组中
            }
            return arrNum;
        }

        public static int getNum2(int[] arrNum, int tmp, int minValue, int maxValue, Random ra)
        {
            int n = 0;
            while (n <= arrNum.Length - 1)
            {
                if (arrNum[n] == tmp) //利用循环判断是否有重复
                {
                    tmp = ra.Next(minValue, maxValue); //重新随机获取。
                    getNum2(arrNum, tmp, minValue, maxValue, ra);//递归:如果取出来的数字和已取得的数字有重复就重新随机获取。
                }
                n = n + 1;
            }
            return tmp;
        }


    }
}
